<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/14 0014
 * Time: 17:16
 */

namespace App\Http\Controllers\Admin;


use App\Repositories\Admin\CategorysTypeRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CategorysTypeController extends Controller
{
    protected $categorysTypeRepository;
    public function __construct(CategorysTypeRepository $categorysTypeRepository)
    {
        $this->categorysTypeRepository = $categorysTypeRepository;
    }
    public function index(){
        $categorysTypes = $this->categorysTypeRepository->getAll();
        if($categorysTypes)
            return json_encode(['code'=>200,'categorysTypes'=>$categorysTypes]);
        else
            return json_encode(['code'=>400,'msg'=>'获取分组类型数据时出错']);
    }
    /**
     * @param Request $request
     * @return string
     */
    public function store(Request $request)
    {
        if($request->has('categorys_type_name')){
            $categorysType = $request->post('categorys_type_name');
            $newCategorysType = [
                'categorys_type_name' => $categorysType,
                'categorys_type_status' => 1,
            ];
            $result = $this->categorysTypeRepository->createCategorysType($newCategorysType);
            if($request)
                return json_encode(['code'=>200,'categorys_type_id'=>$result->categorys_type_id]);
            else
                return json_encode(['code'=>400,'msg'=>'保存分组类型数据时出错']);
        }else{
            return json_encode(['code'=>400,'msg'=>'没有传入相应的分组类型数据']);
        }
    }
}